package cn.wolfcode.crm.service.impl;


import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.ClientDevelopmentPlan;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.ClientDevelopmentPlanMapper;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.mapper.ClientRecordMapper;
import cn.wolfcode.crm.query.ClientDevelopmentPlanObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IClientDevelopmentPlanService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ClientDevelopmentPlanServiceImpl implements IClientDevelopmentPlanService {
    @Autowired
    private ClientDevelopmentPlanMapper clientDevelopmentPlanMapper;
    @Autowired
    private ClientRecordMapper clientRecordMapper;
    @Autowired ClientMapper clientMapper;
    public void save(ClientDevelopmentPlan clientDevelopmentPlan) {
        //获取客户
        Client client = clientDevelopmentPlan.getClient();
        //更改客户的状态
        //设置创建人
        clientDevelopmentPlan.setOriginator((Employee) SecurityUtils.getSubject().getPrincipal());
        //设置创建时间
        clientDevelopmentPlan.setOperationDate(new Date());
        //设置开发状态
        clientDevelopmentPlan.setSchedule(1);
        clientDevelopmentPlanMapper.insert(clientDevelopmentPlan);
    }

    public void delete(Long id) {
        clientDevelopmentPlanMapper.deleteByPrimaryKey(id);

    }

    public void update(ClientDevelopmentPlan clientDevelopmentPlan) {
        clientDevelopmentPlan.setModificationDate(new Date());
        clientDevelopmentPlanMapper.updateByPrimaryKey(clientDevelopmentPlan);
        int schedule = clientDevelopmentPlan.getSchedule();
        if(schedule==0){



            //clientRecordMapper.insertByClientRecordOeder();

        }
    }

    public ClientDevelopmentPlan get(Long id) {
        return clientDevelopmentPlanMapper.selectByPrimaryKey(id);
    }

    public List<ClientDevelopmentPlan> getAll() {

        return clientDevelopmentPlanMapper.selectAll();
    }

    public PageResult<ClientDevelopmentPlan> query(ClientDevelopmentPlanObjectQuery qo) {
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        boolean hasRole = SecurityUtils.getSubject().hasRole("MGR CK");
        if (!employee.isAdmin()||!hasRole){

        qo.setCheckName(employee.getUsername());

        }
        int totalCount = clientDevelopmentPlanMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult<ClientDevelopmentPlan>(totalCount, Collections.<ClientDevelopmentPlan>emptyList());
        }
        //获取当前员工



        return new PageResult<ClientDevelopmentPlan>(totalCount,clientDevelopmentPlanMapper.queryForList(qo));
    }

}
